home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / ACK3D.ZIP / DEMO / DEMORD.C < prev    next >
Text File  |  1993-08-24  |  10KB  |  458 lines

  1. /*********************( Animation Construction Kit Demo )**********************/
  2. /*                Lary Myers                      */
  3. /*                                          */
  4. /* This module reads in the ASCII description file which describes all the    */
  5. /* wall and object bitmaps, object definitions, sound files, and color          */
  6. /* ranges for light shading. It was done this way vs using one big resource   */
  7. /* file so that it could be easily modified to try various bitmaps and such.  */
  8. /* A commercial style game would want to combine all this information into    */
  9. /* common files which could then be compressed, etc to save disk space.          */
  10. /*                                          */
  11. /*****************************<      ACK-3D   >***********************************/
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include <mem.h>
  16. #include <alloc.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <time.h>
  20. #include <string.h>
  21. #include <conio.h>
  22. #include <ctype.h>
  23. #include <sys\stat.h>
  24. #include "ack3d.h"
  25. #include "acksnd.h"
  26. #include "demo.h"
  27.  
  28. /* Variables contained in DEMO.C */
  29. extern    ACKENG    *ae;
  30. extern    char far *SoundFiles[];
  31. extern    char     MusicFile[];
  32.  
  33.  
  34. /****** G L O B A L S ******************************/
  35.         int            ErrorCode;
  36.         char        GridFile[128];
  37.         char        PalFile[128];
  38.  
  39.  
  40. /* Used by the light shading routines */
  41. typedef struct
  42.     {
  43.     unsigned char start;
  44.     unsigned char length;
  45.     }ColorRange;
  46.  
  47.  
  48. /****************************************************************************
  49. ** Return just the extension portion of the filename.               **
  50. ****************************************************************************/
  51. char *GetExtent(char *s)
  52. {
  53.     char    *e;
  54.  
  55. e = strchr(s,'.');
  56. if (e == NULL)
  57.     return(s);
  58. e++;
  59.  
  60. return(e);
  61. }
  62.  
  63. /****************************************************************************
  64. ** Remove any linefeeds or carriage returns from the string           **
  65. ****************************************************************************/
  66. char *StripEndOfLine(char *s)
  67. {
  68.     int        len;
  69.     char    ch;
  70.  
  71. len = strlen(s);
  72.  
  73. while (--len >= 0)
  74.     {
  75.     ch = s[len];
  76.     if (ch != ' ' && ch != ';' && ch != '\t' && ch != 13 && ch != 10)
  77.     break;
  78.  
  79.     s[len] = '\0';
  80.     }
  81.  
  82. return(s);
  83. }
  84.  
  85. /****************************************************************************
  86. ** Skip an leading spaces in the string                       **
  87. ****************************************************************************/
  88. char *SkipSpaces(char *s)
  89. {
  90.  
  91. while (*s == ' ' || *s == '\t' || *s == ',')
  92.     strcpy(s,&s[1]);
  93.  
  94. return(s);
  95. }
  96.  
  97. /****************************************************************************
  98. ** Add specified extent if needed onto the end of the filename.           **
  99. ****************************************************************************/
  100. char *AddExtent(char *s,char *ext)
  101. {
  102. if (strchr(s,'.') == NULL)
  103.     strcat(s,ext);
  104.  
  105. return(s);
  106. }
  107.  
  108. /****************************************************************************
  109. ** Copies source to destination until delimiter is found.           **
  110. ****************************************************************************/
  111. char *CopyToComma(char *dest,char *src)
  112. {
  113.     char    ch;
  114.  
  115. while (*src)
  116.     {
  117.     ch = *src++;
  118.     if (ch == ' ' || ch == '\t' || ch == ',')
  119.     break;
  120.  
  121.     *dest++ = ch;
  122.     }
  123.  
  124. *dest = '\0';
  125.  
  126. return(src);
  127. }
  128.  
  129.  
  130. /****************************************************************************
  131. ** Reads and parses the ASCII file containing the demo information.       **
  132. **                                       **
  133. ****************************************************************************/
  134. int ReadMasterFile(char *fname)
  135. {
  136.     FILE    *fp;
  137.     int        bType,bNum,result;
  138.     int        value,ObjIndex;
  139.     int        Mode = 0;
  140.     int     fMode = 0,i,j,found,k;
  141.     char    *s;
  142.     char    bName[128];
  143.     char    fBuf[80];
  144.     char    rangenos = 0,plotcolor;   //**MOD
  145.     ColorRange ranges[64];  //**MOD
  146.  
  147. fp = fopen(fname,"rt");
  148. if (fp == NULL)
  149.     {
  150.     printf("Master file %s not found.\n");
  151.     ErrorCode = ERR_BADFILE;
  152.     return(-1);
  153.     }
  154.  
  155. bNum = 1;
  156. bType = TYPE_WALL;
  157. result = 0;
  158.  
  159. while (!result)
  160.     {
  161.     if (feof(fp))
  162.     break;
  163.  
  164.     *bName = '\0';
  165.     fgets(bName,127,fp);
  166.  
  167.     if (*bName == ';')
  168.     continue;
  169.  
  170.     StripEndOfLine(bName);
  171.     SkipSpaces(bName);
  172.  
  173.     if (!strlen(bName))
  174.     continue;
  175.  
  176.     if (*bName == ';')
  177.     continue;
  178.  
  179.     if (!strnicmp(bName,"SOUND:",6))
  180.     {
  181.     value = atoi(&bName[7]);
  182.     if (value < 0 || value > SOUND_MAX_INDEX)
  183.         {
  184.         printf("Invalid sound index: %s\n",bName);
  185.         ErrorCode = ERR_BADCOMMAND;
  186.         result = -1;
  187.         }
  188.  
  189.     strcpy(bName,SkipSpaces(&bName[7]));
  190.  
  191.     s = strpbrk(bName,", \t");
  192.     if (s == NULL)
  193.         {
  194.         printf("Unable to locate name for sound file: %s.\n",bName);
  195.         ErrorCode = ERR_BADSYNTAX;
  196.         result = -1;
  197.         continue;
  198.         }
  199.  
  200.     strcpy(fBuf,SkipSpaces(s));
  201.     AddExtent(fBuf,".voc");
  202.     SoundFiles[value] = malloc(strlen(fBuf)+1);
  203.     if (SoundFiles[value] == NULL)
  204.         {
  205.         ErrorCode = ERR_NOMEMORY;
  206.         result = -1;
  207.         continue;
  208.         }
  209.  
  210.     strcpy(SoundFiles[value],fBuf);
  211.     continue;
  212.     }
  213.  
  214.     if (!strnicmp(bName,"MUSIC:",6))
  215.     {
  216.     strcpy(MusicFile,SkipSpaces(&bName[6]));
  217.     continue;
  218.     }
  219.  
  220.  
  221.  
  222.     if (!stricmp(bName,"WALLS:"))
  223.     {
  224.     bType = TYPE_WALL;
  225.     bNum = 1;
  226.     Mode = 1;
  227.     continue;
  228.     }
  229.  
  230.     if (!stricmp(bName,"ENDWALLS:"))
  231.     {
  232.     if (Mode != 1)
  233.         {
  234.         printf("Invalid place for command: %s.\n",bName);
  235.         ErrorCode = ERR_BADCOMMAND;
  236.         result = -1;
  237.         }
  238.  
  239.     Mode = 0;
  240.     continue;
  241.     }
  242.  
  243.     if (!stricmp(bName,"OBJECTS:"))
  244.     {
  245.     bType = TYPE_OBJECT;
  246.     bNum = 40;
  247.     ObjIndex = 1;
  248.     Mode = 2;
  249.     continue;
  250.     }
  251.  
  252.     if (!stricmp(bName,"FILES:"))
  253.     {
  254.     fMode = 1;
  255.     continue;
  256.     }
  257.  
  258.     if (!stricmp(bName,"ENDFILES:"))
  259.     {
  260.     fMode = 0;
  261.     continue;
  262.     }
  263.  
  264.     if (fMode)
  265.     {
  266.     value = atoi(bName);
  267.     if (value < 1 || value > 255)
  268.         {
  269.         printf("Invalid number for object: %s.\n",bName);
  270.         ErrorCode = ERR_BADOBJNUMBER;
  271.         result = -1;
  272.         continue;
  273.         }
  274.  
  275.     s = strpbrk(bName,", \t");
  276.     if (s == NULL)
  277.         {
  278.         printf("Unable to locate bitmap name for object: %s.\n",bName);
  279.         ErrorCode = ERR_BADSYNTAX;
  280.         result = -1;
  281.         continue;
  282.         }
  283.  
  284.     strcpy(fBuf,SkipSpaces(s));
  285.     AddExtent(fBuf,".img");
  286.  
  287.     if (AckLoadBitmap(ae,value,bType,fBuf))
  288.         {
  289.         printf("Error loading bitmap \"%s\".\n",fBuf);
  290.  
  291.     #if 0
  292.         ErrorCode = ERR_LOADINGBITMAP;
  293.     #endif
  294.         result = -1;
  295.         }
  296.     continue;
  297.     }
  298.  
  299.  
  300.  
  301.     if (Mode == 2)
  302.     {
  303.     if (!strnicmp(bName,"NUMBER:",7))
  304.         {
  305.         value = atoi(&bName[7]);
  306.  
  307.         if (value < 1 || value >= MAX_OBJECTS)
  308.         {
  309.         printf("Invalid object number:\n%s\n",bName);
  310.         ErrorCode = ERR_BADOBJNUMBER;
  311.         result = -1;
  312.         break;
  313.         }
  314.         ObjIndex = value;
  315.         continue;
  316.         }
  317.  
  318.     if (!strnicmp(bName,"DIRECTION:",10))
  319.         {
  320.         value = atoi(&bName[10]);
  321.         if (value < 0 || value > 10)
  322.         {
  323.         printf("Invalid direction: %d.\n",value);
  324.         ErrorCode = ERR_BADDIRECTION;
  325.         result = -1;
  326.         continue;
  327.         }
  328.  
  329.         ae->ObjList[ObjIndex].Dir = value;
  330.         if (value == 10)
  331.         {
  332.         ae->ObjList[ObjIndex].Sides = 0;
  333.         ae->ObjList[ObjIndex].Flags |= OF_ANIMATE;
  334.         }
  335.         continue;
  336.         }
  337.  
  338.     if (!strnicmp(bName,"SPEED:",6))
  339.         {
  340.         ae->ObjList[ObjIndex].Speed = atoi(&bName[6]);
  341.         continue;
  342.         }
  343.  
  344.     if (!strnicmp(bName,"FLAGS:",6))
  345.         {
  346.         strupr(bName);
  347.         if (strstr(bName,"PASSABLE") != NULL)
  348.         {
  349.         ae->ObjList[ObjIndex].Flags |= OF_PASSABLE;
  350.         }
  351.         continue;
  352.         }
  353.  
  354.  
  355.     if (!strnicmp(bName,"BITMAPS:",8))
  356.         {
  357.         strcpy(bName,SkipSpaces(&bName[8]));
  358.         value = 0;
  359.         while (value < MAX_VIEWS && *bName)
  360.         {
  361.         strcpy(bName,CopyToComma(fBuf,bName));
  362.         SkipSpaces(fBuf);
  363.         bNum = atoi(fBuf);
  364.  
  365.         if (bNum < 1 || bNum > 255)
  366.             {
  367.             printf("Invalid bitmap number for object: %d\n",ObjIndex);
  368.             ErrorCode = ERR_BADOBJNUMBER;
  369.             result = -1;
  370.             break;
  371.             }
  372.  
  373.         ae->ObjList[ObjIndex].bmNum[value] = bNum;
  374.         value++;
  375.         }
  376.  
  377.         AckCreateObject(ae,ObjIndex,value,ae->ObjList[ObjIndex].bmNum);
  378.         continue;
  379.         }
  380.  
  381.     if (!strnicmp(bName,"ENDOBJECTS:",11))
  382.         {
  383.         Mode = 0;
  384.         continue;
  385.         }
  386.  
  387.     }
  388.  
  389.     if (!strnicmp(bName,"RANGE:",6))
  390.         {
  391.         int start,colnum;
  392.         strcpy(bName,SkipSpaces(&bName[6]));
  393.         strcpy(bName,CopyToComma(fBuf,bName));
  394.         SkipSpaces(fBuf);
  395.     start =atoi(fBuf);
  396.         strcpy(bName,CopyToComma(fBuf,bName));
  397.         SkipSpaces(fBuf);
  398.     colnum=atoi(fBuf);
  399.     ranges[rangenos].start    = start;
  400.     ranges[rangenos].length = colnum;
  401.     rangenos ++;
  402.     continue;
  403.     }
  404.  
  405.     if (!strnicmp(bName,"PALFILE:",8))
  406.     {
  407.     strcpy(PalFile,SkipSpaces(&bName[8]));
  408.     continue;
  409.     }
  410.  
  411.     if (!strnicmp(bName,"MAPFILE:",8))
  412.     {
  413.     strcpy(GridFile,SkipSpaces(&bName[8]));
  414.     continue;
  415.     }
  416.  
  417.     }
  418.  
  419. fclose(fp);
  420.  
  421. /* Setup the light shading ranges for use by the engine */
  422.  
  423. for ( i = 0;i<16;i++)
  424.     {
  425.     for (j=0;j<256;j++)
  426.     {
  427.     found = 0;
  428.     // find the range the color is in.
  429.     for ( k = 0; k < rangenos; k++ )
  430.         {
  431.         if (j >= ranges[k].start && j < ranges[k].start+ranges[k].length)
  432.         {
  433.         found = 1;
  434.         break;
  435.         }
  436.         }
  437.     if (found)
  438.         {
  439.         // add color + i;
  440.         // if color + i > color+range then plot color = 0;
  441.         // otherwise plotcolor = color+i
  442.         if (j+i >= ranges[k].start+ranges[k].length)
  443.            plotcolor = 0;
  444.         else
  445.            plotcolor = j+i;
  446.         }
  447.     else
  448.         {
  449.         plotcolor = j;
  450.         }
  451.     ae->PalTable[j+(i*256)] = plotcolor;
  452.     }
  453.     }
  454.  
  455. return(result);
  456. }
  457.  
  458.